package com.example.androidtvdemo.demo05;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;
import androidx.leanback.widget.ArrayObjectAdapter;
import androidx.leanback.widget.FocusHighlight;
import androidx.leanback.widget.FocusHighlightHelper;
import androidx.leanback.widget.ItemBridgeAdapter;
import androidx.leanback.widget.VerticalGridView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.androidtvdemo.BuildConfig;
import com.example.androidtvdemo.R;
import com.example.androidtvdemo.net.BlogCategory;
import com.example.androidtvdemo.net.BlogSubArticle;
import com.example.androidtvdemo.net.HttpManager;
import com.example.androidtvdemo.net.IappService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class Demo05Activity extends FragmentActivity {
    private static final String TAG = "Demo05Activity";
    private TextView tvCategory;
    private Button btnCollection;
    private RecyclerView rvCategory;
    private VerticalGridView vgv;
    private CategoryAdapter mCategoryAdapter;
    private ArrayObjectAdapter mAdapter;
    private Call<BlogSubArticle> subArticleCall;
    IappService service = HttpManager.getService(IappService.class, BuildConfig.APP_BASE_URL);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo05);
        initView();
        //获取数据
        service.getCategoryList()
                .enqueue(new Callback<BlogCategory>() {
                    @Override
                    public void onResponse(Call<BlogCategory> call, Response<BlogCategory> response) {
                        //获取成功
                        mCategoryAdapter.setNewInstance(response.body().getData());
                    }

                    @Override
                    public void onFailure(Call<BlogCategory> call, Throwable t) {

                    }
                });

    }

    private void initView() {
        tvCategory = (TextView) findViewById(R.id.tv_category);
        btnCollection = (Button) findViewById(R.id.btn_collection);
        rvCategory = (RecyclerView) findViewById(R.id.rv_category);
        vgv = (VerticalGridView) findViewById(R.id.vgv);
        mCategoryAdapter = new CategoryAdapter();

        mCategoryAdapter.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (!hasFocus) {
                    return;
                }
                int categoryIndex = (int) v.getTag();
                BlogCategory.DataCategoryBean bean = mCategoryAdapter.getData().get(categoryIndex);
                getArticleById(bean.getId());
                tvCategory.setText(bean.getName());
            }
        });

        rvCategory.setLayoutManager(new LinearLayoutManager(this));
        rvCategory.setAdapter(mCategoryAdapter);

        vgv.setNumColumns(3);
        //初始化列表需要的adapter
        mAdapter = new ArrayObjectAdapter(new ArticleTestPresenter());
        ItemBridgeAdapter iba = new ItemBridgeAdapter(mAdapter);
        vgv.setAdapter(iba);
        //选中放大效果增加这个代码,flat中有放大比例
        FocusHighlightHelper.setupBrowseItemFocusHighlight(iba, FocusHighlight.ZOOM_FACTOR_SMALL, false);

    }

    private void getArticleById(String id) {
        if (subArticleCall != null && subArticleCall.isExecuted()) {
            //如果有在执行中，就先取消
            subArticleCall.cancel();
        }
        subArticleCall = service.getArticleById(id);
        //请求博客文章数据
        subArticleCall.enqueue(new Callback<BlogSubArticle>() {
            @Override
            public void onResponse(Call<BlogSubArticle> call, Response<BlogSubArticle> response) {
                //数据获取成功之后,显示图片数据
                //更新数据
//                Log.i(TAG, "onResponse: " + response.body().getSuccess());

                mAdapter.setItems(response.body().getData().getContent(), null);
            }

            @Override
            public void onFailure(Call<BlogSubArticle> call, Throwable t) {
                t.printStackTrace();
            }
        });
    }
}